package com.lianbang.pharmacy.vm

import androidx.lifecycle.MutableLiveData
import cn.ubest.mvvmcore.base.BaseListViewModel
import cn.ubest.mvvmcore.base.BaseViewModel
import cn.ubest.mvvmcore.bean.Wrapper
import cn.ubest.mvvmcore.interfaces.RequestHandler1Impl
import com.lianbang.pharmacy.bean.AddressListBean
import com.lianbang.pharmacy.model.UserModel

/**
 * Created by maoqi on 2020/12/30.
 */
class AddressVM : BaseListViewModel<UserModel>() {
    val mAddressLiveData by lazy {
        MutableLiveData<Any>()
    }

    private fun addressList() {
        requestWithLife(object : RequestHandler1Impl<List<AddressListBean>>(this) {
            override suspend fun request1(): List<AddressListBean> {
                return mModel.addressList().resolveWrapper()
            }

            override fun onSuccess(t: List<AddressListBean>) {
                setData(t)
            }
        })
    }

    fun addOrChangeAddress(address: AddressListBean) {
        requestWithLife(object : RequestHandler1Impl<Wrapper>(this) {
            override suspend fun request1(): Wrapper {
                return mModel.addOrChangeAddress(address)
            }

            override fun onSuccess(t: Wrapper) {
                mAddressLiveData.postValue(true)
            }
        })
    }

    fun deleteAddress(id: Int, position: Int) {
        requestWithLife(object : RequestHandler1Impl<Wrapper>(this) {
            override suspend fun request1(): Wrapper {
                return mModel.deleteAddress(id)
            }

            override fun onSuccess(t: Wrapper) {
                mAddressLiveData.postValue(position)
            }
        })
    }

    override fun loadData(isInit: Boolean, vararg arg: Any) {
        addressList()
    }
}